//
//  String+Extension.swift
//  chat
//
//  Created by 崔世强 on 2018/10/29.
//  Copyright © 2018 崔世强. All rights reserved.
//

import Foundation

extension String {
    func substring(s: Int, _ e: Int? = nil) -> String {
        let start = s >= 0 ? self.startIndex : self.endIndex
        let startIndex = self.index(start, offsetBy: s)
        
        var end: String.Index
        var endIndex: String.Index
        if(e == nil){
            end = self.endIndex
            endIndex = self.endIndex
        } else {
            end = e ?? 0 >= 0 ? self.startIndex : self.endIndex
            endIndex = self.index(end, offsetBy: e!)
        }
        
        let range = startIndex..<endIndex
        return String(self[range])
        
    }
    
    func isPhoneNumber() -> Bool {
        if self.count == 0 {
            return false
        }
        let mobile = "1[34578]([0-9]){9}"
        let regexMobile = NSPredicate(format: "SELF MATCHES %@",mobile)
        return regexMobile.evaluate(with: self)
    }
    
}
